/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.suli.tasks.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.annotation.rest.AnonymousPostMapping;
import me.zhengjie.suli.tasks.domain.Tasks;
import me.zhengjie.suli.tasks.service.TasksService;
import me.zhengjie.suli.tasks.service.dto.TasksQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author Allen Gao
 * @website https://eladmin.vip
 * @date 2022-12-30
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "任务列表管理")
@RequestMapping("/api/tasks")
public class TasksController {

    private final TasksService tasksService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('tasks:list')")
    public void exportTasks(HttpServletResponse response, TasksQueryCriteria criteria) throws IOException {
        tasksService.download(tasksService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询任务列表")
    @ApiOperation("查询任务列表")
    @PreAuthorize("@el.check('tasks:list')")
    public ResponseEntity<Object> queryTasks(TasksQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(tasksService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增任务列表")
    @ApiOperation("新增任务列表")
    @PreAuthorize("@el.check('tasks:add')")
    public ResponseEntity<Object> createTasks(@Validated @RequestBody Tasks resources) {
        return new ResponseEntity<>(tasksService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改任务列表")
    @ApiOperation("修改任务列表")
    @PreAuthorize("@el.check('tasks:edit')")
    public ResponseEntity<Object> updateTasks(@Validated @RequestBody Tasks resources) {
        tasksService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除任务列表")
    @ApiOperation("删除任务列表")
    @PreAuthorize("@el.check('tasks:del')")
    public ResponseEntity<Object> deleteTasks(@RequestBody String[] ids) {
        tasksService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/manualCompletion")
    @Log("手动完成任务")
    @ApiOperation("手动完成任务")
    public ResponseEntity<Object> manualCompletion(List<String> ids){
        return new ResponseEntity<>(tasksService.manualCompletion(ids), HttpStatus.OK);
    }

    @PostMapping("/reZuPan")
    @Log("重新组盘")
    @ApiOperation("手动完成任务")
    public ResponseEntity<Object> reZuPan(@RequestBody String param){
        return new ResponseEntity<>(tasksService.reZuPan(param), HttpStatus.OK);
    }

    @PostMapping("/rePush")
    @Log("重新执行库内转换任务")
    @ApiOperation("重新执行库内转换任务")
    public ResponseEntity<Object> rePush(@RequestBody String param){
        return new ResponseEntity<>(tasksService.rePush(param), HttpStatus.OK);
    }

    @PostMapping("/setOk")
    @Log("重新执行库内转换任务")
    @ApiOperation("重新执行库内转换任务")
    public ResponseEntity<Object> setOk(@RequestBody String trayCode){
        return new ResponseEntity<>(tasksService.setOk(trayCode), HttpStatus.OK);
    }
    @PostMapping("/setFail")
    @Log("重新执行库内转换任务")
    @ApiOperation("重新执行库内转换任务")
    public ResponseEntity<Object> setFail(@RequestBody String trayCode){
        return new ResponseEntity<>(tasksService.setFail(trayCode), HttpStatus.OK);
    }
}